<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :before-close="closeUpdate">
    <el-form :model="dataForm"  ref="dataForm" label-width="80px">
    <el-form-item label="发送人" prop="senderId">
      <el-input v-model="dataForm.sender"  :readonly="true"></el-input>
    </el-form-item>
      <el-form-item label="标题" prop="msgInfoId">
        <el-input v-model="dataForm.msgInfoEntity.title"  :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input
          :readonly="true"
          type="textarea"
          placeholder="内容"
          v-model="dataForm.msgInfoEntity.content"
          show-word-limit
          :rows="5"
        >
        </el-input>
      </el-form-item>
    <el-form-item label="发送时间" prop="createTime">
      <el-input v-model="dataForm.createTime"  :readonly="true"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeUpdate">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          msgId: 0,
          sender: '',
          receiver: '',
          msgInfoEntity:{},
          status: '',
          createTime: ''
        }
      }
    },
    computed: {
      msgNoRead: {
        get () { return this.$store.state.user.msgNoRead},
        set (val) { this.$store.commit('user/msgNoRead', val) },
      }
    },
    methods: {
      init (id) {
        this.dataForm.msgId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.msgId) {
            this.$http({
              url: this.$http.adornUrl(`/msg/message/info/${this.dataForm.msgId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.msgId = data.message.msgId
                this.dataForm.sender = data.message.sender
                this.dataForm.receiver = data.message.receiver
                this.dataForm.msgInfoEntity = data.message.msgInfoEntity
                this.dataForm.status = data.message.status
                this.dataForm.createTime = data.message.createTime
                this.dataForm.updateTime = data.message.updateTime
                this.dataForm.createBy = data.message.createBy
                this.dataForm.updateBy = data.message.updateBy

                if(this.dataForm.status==0) {
                  this.$http({
                    url: this.$http.adornUrl(`/msg/message/${!this.dataForm.msgId ? 'save' : 'update'}`),
                    method: 'post',
                    data: this.$http.adornData({
                      'msgId': this.dataForm.msgId || undefined,
                      'sender': this.dataForm.sender,
                      'receiver': this.dataForm.receiver,
                      'msgInfoId': this.dataForm.msgInfoId,
                      'status': 1
                    })
                  }).then((data)=>{
                    this.$http({
                      url: this.$http.adornUrl('/sys/user/info'),
                      method: 'get',
                      params: this.$http.adornParams()
                    }).then(({data}) => {
                      if (data && data.code === 0) {
                        this.loading = false
                        this.msgNoRead = data.user.msgNoRead
                      }
                    })
                  })
                }
              }
            })
          }
        })
      },
      closeUpdate(){
        this.visible = false
        this.$emit('refreshDataList')
      },

    }
  }
</script>
